package DFS;

/**
 * @Classname 岛屿的最大的面积695
 * @Description TODO
 * @Date 2023/3/15 21:13
 * @Created by xjl
 */
public class 岛屿的最大的面积695 {
    int[][] dist=new int[][]{{1,0},{0,1},{-1,0},{0,-1}};

    public int maxAreaOfIsland(int[][] grid) {
        int res = 0;
        for (int r = 0; r < grid.length; r++) {
            for (int c = 0; c < grid[0].length; c++) {
                if (grid[r][c] == 1) {
                    int a = dfs(grid, r, c);
                    res = Math.max(res, a);
                }
            }
        }
        return res;
    }

    private int dfs(int[][] grid, int i, int j) {
        if (i<0||i>=grid.length||j<0||j>=grid[0].length||grid[i][j]!=1){
            return 0;
        }
        grid[i][j]=0;
        int count=1;
        for (int k=0;k<4;k++){
            int x=i+dist[k][0];
            int y=j+dist[k][1];
            count+=dfs(grid,x,y);
        }
        return count;
    }
}
